package mapper.impl;

import entity.Admin;
import mapper.IAdmMapper;
import util工具包.JDBCutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public  class AdmMapperImpl implements IAdmMapper {
    @Override//注册
    public boolean add(String t_No, String name, String account, String password) {
//        boolean result = false;
        Connection conn = JDBCutil.getconnection();
        int a ;
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement("INSERT INTO user_admin (t_No,name,account,password) values(?,?,?,?)");
            pst.setString(1, t_No);
            pst.setString(2, name);
            pst.setString(3, account);
            pst.setString(4, password);

            a = pst.executeUpdate();
            if (a == 0) {
                System.out.println("存储失败");
                return false;
            } else {
                System.out.println("成功存储了"+a+"条数据");
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
            JDBCutil.close(conn,pst,null);
        }
        return false;
    }


    @Override//登录
    public  Admin login(String account, String password) {
        Connection conn = JDBCutil.getconnection();
        PreparedStatement pst=null;
        ResultSet rs = null;
        Admin admin = new Admin();
        try {
            pst = conn.prepareStatement("select account,password from user_admin  ");
            rs=pst.executeQuery();
            if (rs.next()) {
                String account1 = rs.getString("account");
                String pass = rs.getString("password");
                if (account.equals(account1)) {
                    admin.setAccount(account1);
                    admin.setPassword(pass);
                    return admin;
                }
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCutil.close(conn,pst,rs);
        }
        return null;
    }
}


